﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.EffiProz;

namespace EffiProz.Explorer
{
    /// <summary>
    /// Interaction logic for Connect.xaml
    /// </summary>
    public partial class Connect : Window
    {
        MainWindow _parent;
        public Connect()
        {
            InitializeComponent();
            _parent = null;
        }

        public Connect(MainWindow parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void typeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (typeComboBox.SelectedIndex == 0)
                {
                    browseButton.IsEnabled = false;
                    hostLabel.IsEnabled = false;
                    portLabel.IsEnabled = false;
                    hostTextBox.IsEnabled = false;
                    portTextBox.IsEnabled = false;
                    sslCheckBox.IsEnabled = false;
                    grid1.RowDefinitions[2].Height = new GridLength(0);
                    grid1.RowDefinitions[3].Height = new GridLength(0);
                    this.Height = 230;
                }
                else if (typeComboBox.SelectedIndex == 1)
                {
                    browseButton.IsEnabled = true;
                    hostLabel.IsEnabled = false;
                    portLabel.IsEnabled = false;
                    hostTextBox.IsEnabled = false;
                    portTextBox.IsEnabled = false;
                    sslCheckBox.IsEnabled = false;
                    grid1.RowDefinitions[2].Height = new GridLength(0);
                    grid1.RowDefinitions[3].Height = new GridLength(0);
                    this.Height = 230;
                }
                else if (typeComboBox.SelectedIndex == 2 || typeComboBox.SelectedIndex == 3)
                {
                    browseButton.IsEnabled = false;
                    hostLabel.IsEnabled = true;
                    portLabel.IsEnabled = true;
                    hostTextBox.IsEnabled = true;
                    portTextBox.IsEnabled = true;
                    sslCheckBox.IsEnabled = true;
                    grid1.RowDefinitions[2].Height = new GridLength(35);
                    grid1.RowDefinitions[3].Height = new GridLength(35);
                    this.Height = 300;

                    hostTextBox.Text = String.IsNullOrEmpty(hostTextBox.Text) ? "localhost" : hostTextBox.Text;
                    portTextBox.Text = String.IsNullOrEmpty(portTextBox.Text) ?(typeComboBox.SelectedIndex == 2 ? "9001" : "80") : portTextBox.Text;                  
                }
            }
            catch (Exception) { }

        }

        private void browseButton_Click(object sender, RoutedEventArgs e)
        {
            // Configure open file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = ""; // Default file name
            dlg.DefaultExt = ".properties"; // Default file extension
            dlg.Filter = "EffiProz Database Properties File (.properties)|*.properties"; // Filter files by extension

            bool? result = dlg.ShowDialog();

            if (result == true)
            {
                databaseTextBox.Text =  dlg.FileName.Substring(0,dlg.FileName.Length -11);
            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            EfzConnection conn= null;
            EfzConnectionStringBuilder connBuilder = null;

            string databaseUrl = String.Empty;
            try
            {
                connBuilder = new EfzConnectionStringBuilder();                

                switch (typeComboBox.SelectedIndex)
                {
                    case 0:
                        databaseUrl = connBuilder.ConnectionType = "memory";                        
                        break;
                    case 1:
                        databaseUrl = connBuilder.ConnectionType = "file";                        
                        break;
                    case 2:
                        databaseUrl = connBuilder.ConnectionType = "server";
                        connBuilder.DataSource = hostTextBox.Text + ":" +
                            int.Parse(portTextBox.Text);

                        databaseUrl += ":" + connBuilder.DataSource;
                        if (sslCheckBox.IsChecked == true)
                        {
                            databaseUrl += ":ssl";
                            connBuilder.Ssl = true;
                        }
                        break;
                    case 3:
                        databaseUrl = connBuilder.ConnectionType = "webserver";
                        connBuilder.DataSource =  hostTextBox.Text + ":" +
                            int.Parse(portTextBox.Text);

                        databaseUrl += ":" + connBuilder.DataSource;
                        if (sslCheckBox.IsChecked == true)
                        {
                            databaseUrl += ":ssl";
                            connBuilder.Ssl = true;
                        }
                        break;
                    default:
                        throw new Exception("Invalid connection type.");
                }
                if (String.IsNullOrEmpty(databaseTextBox.Text))
                    throw new Exception("Invalid datasource");

                connBuilder.InitialCatalog = databaseTextBox.Text;
                databaseUrl += "://" + connBuilder.InitialCatalog;
                connBuilder.User = userTextBox.Text;
                connBuilder.Password = passwordTextBox.Password;
                

                conn = new EfzConnection(connBuilder.ConnectionString);
                conn.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to connect to the database.\n Error: "+ex.Message, "Connection Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            _parent.SetConnection(conn);
            _parent.SetDatabaseUrl(databaseUrl);
            this.DialogResult = true;
            this.Close();
        }
     
    }
}
